home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 13 - 1997 (partial) / 13.02 Feb 97 / HappyFace, a CTB Game / Main.c < prev   
Encoding:
C/C++ Source or Header  |  1996-10-17  |  7.3 KB  |  306 lines  |  [TEXT/CWIE]

  1. // Main.c - Does the application initialization, event loop
  2. //            and high-level interface maintenance
  3.  
  4. #include "CTB.h"
  5. #include "Game.h"
  6.  
  7. // event masks
  8. #define kResumeMask        0x010000000
  9.  
  10. // mbar
  11. #define rMBarID            256
  12.  
  13. // menus
  14. #define rAppleMenuID    256
  15. #define rFileMenuID        257
  16. #define rEditMenuID        258
  17. #define rTestMenuID        260
  18. // see also CTB.h
  19.  
  20. // menu items
  21. #define rAboutMItem        1
  22. #define rNewGameItem    1
  23. #define rQuitMItem        3
  24. #define rTryMItem        1
  25.  
  26. // about alert
  27. #define rAboutAlertID    128
  28.  
  29. // cursors
  30. #define rHammerCurs        128
  31.  
  32. // globals
  33. Handle        gMBar;
  34. MenuHandle    gAppleMenuH, gFileMenuH, gEditMenuH, gConnMenuH;
  35. WindowPtr    gGameWindow;
  36. CursHandle    gHammerHand;
  37. Boolean        gGameInProgress, gOnline;
  38.  
  39. //**********************************************************
  40. //
  41. // ExitNow - Does stuff that should happen before ExitToShell
  42. //
  43. //**********************************************************
  44. pascal void ExitNow(void)
  45. {
  46.     DisposeConn();        // close connection-related stuff
  47.     ExitToShell();
  48. }
  49.  
  50. //**********************************************************
  51. //
  52. // DoQuit - Causes a normal exit
  53. //
  54. //**********************************************************
  55. pascal void DoQuit(void)
  56. {
  57.     DisposeConn();        // close connection-related stuff
  58.     if (gGameInProgress)
  59.         ShowScores();
  60.     ExitToShell();
  61. }
  62.  
  63. //**********************************************************
  64. //
  65. // InitSys - Initializes the usual system stuff
  66. //
  67. //**********************************************************
  68. void InitSys(void)
  69. {
  70.     InitGraf(&qd.thePort);
  71.     InitWindows();
  72.     InitFonts();
  73.     InitMenus();
  74.     InitDialogs(ExitNow);
  75.     InitCursor();
  76.     FlushEvents(everyEvent, nil);
  77. }
  78.  
  79. //**********************************************************
  80. //
  81. // SetupMenus - Installs our menus
  82. //
  83. //**********************************************************
  84. void SetupMenus(void)
  85. {
  86.     gMBar = GetNewMBar(rMBarID);
  87.     SetMenuBar(gMBar);
  88.     gAppleMenuH = GetMHandle(rAppleMenuID);
  89.     gFileMenuH = GetMHandle(rFileMenuID);
  90.     gEditMenuH = GetMHandle(rEditMenuID);
  91.     gConnMenuH = GetMHandle(rConnMenuID);
  92.     AddResMenu(gAppleMenuH, 'DRVR');
  93.     DrawMenuBar();
  94. }
  95.  
  96. //**********************************************************
  97. //
  98. // InitApp - Initializes application-specific stuff
  99. //
  100. //**********************************************************
  101. void InitApp(void)
  102. {
  103.     SetupMenus();
  104.     gHammerHand = nil;
  105.     if (gHammerHand = GetCursor(rHammerCurs)) {
  106.         MoveHHi((Handle)gHammerHand);
  107.         HLock((Handle)gHammerHand);
  108.     }
  109.     if (NewConn(kAsync, kDoInitCTB, kDontPrompt)) {
  110.         SysBeep(1);
  111.         ExitNow();    // you will want to report this error
  112.     }
  113.     gGameWindow = NewGameWindow();    // create game window
  114.     gOnline = false;    // initialize variables tracked here
  115.     gGameInProgress = false;
  116. }
  117.  
  118. //**********************************************************
  119. //
  120. // DoMenu - Handles a selection
  121. //
  122. //**********************************************************
  123. void DoMenu(long menuResult)
  124. {
  125.     short         menuID, itemNumber;
  126.     Str255         daName;
  127.     GrafPtr     oldPort;
  128.     
  129.     menuID = HiWord(menuResult);
  130.     itemNumber = LoWord(menuResult);
  131.     
  132.     switch(menuID) {
  133.         case rAppleMenuID:
  134.             if (itemNumber == rAboutMItem) {
  135.                 SetCursor(&qd.arrow);
  136.                 Alert(rAboutAlertID, nil);
  137.             } else {
  138.                 GetItem(gAppleMenuH, itemNumber, daName);
  139.                 GetPort(&oldPort);
  140.                 OpenDeskAcc(daName);
  141.                 SetPort(oldPort);
  142.             }
  143.             break;
  144.         case rFileMenuID:
  145.             switch(itemNumber) {
  146.                 case rNewGameItem:
  147.                     if (SendNewGame())
  148.                         SysBeep(1);    // a reportable error error
  149.                     else
  150.                         NewGame(); // report errors in these calls?
  151.                     gGameInProgress = true;
  152.                     break;
  153.                 case rQuitMItem:
  154.                     DoQuit();
  155.                     break;
  156.             }
  157.             break;
  158.         case rEditMenuID:
  159.             SystemEdit(itemNumber - 1);
  160.             break;
  161.         case rConnMenuID:    // CTB.c maintains this menu
  162.             DoConnMenu(itemNumber);
  163.             break;
  164.         default:
  165.             break;
  166.     }
  167.     HiliteMenu(0);
  168. }
  169.  
  170. //**********************************************************
  171. //
  172. // AdjustCursor - Make sure the hammer showing if necessary
  173. //
  174. //**********************************************************
  175. void AdjustCursor(Point thePoint, RgnHandle theRgn)
  176. {
  177.     if (PtInRgn(thePoint, ((WindowPeek)gGameWindow)->contRgn)) {
  178.         if (gGameInProgress)
  179.             SetCursor(*gHammerHand);
  180.         else
  181.             SetCursor(&qd.arrow);
  182.         CopyRgn(((WindowPeek)gGameWindow)->contRgn, theRgn);
  183.     } else {
  184.         SetCursor(&qd.arrow);
  185.         DiffRgn(GetGrayRgn(), 
  186.             ((WindowPeek)gGameWindow)->contRgn, theRgn);
  187.     }
  188. }
  189.  
  190. //**********************************************************
  191. //
  192. // AdjustMenus - Adjust any menus that change on context
  193. //
  194. //**********************************************************
  195. void AdjustMenus(void)
  196. {
  197.     if (gOnline)
  198.         EnableItem(gFileMenuH, rNewGameItem);
  199.     else
  200.         DisableItem(gFileMenuH, rNewGameItem);
  201. }
  202.  
  203. //**********************************************************
  204. //
  205. // CurMousePoint - Find mouse position in global coordinates
  206. //
  207. //**********************************************************
  208. Point CurMousePoint(void)
  209. {
  210.     Point p;
  211.     
  212.     GetMouse(&p);
  213.     GlobalToLocal(&p);
  214.     
  215.     return p;
  216. }
  217.  
  218. //**********************************************************
  219. //
  220. // main - The obligatory big unit
  221. //
  222. //**********************************************************
  223. void main(void)
  224. {
  225.     EventRecord        theEvent;
  226.     WindowPtr        whichWindow;
  227.     Boolean            gameWasInProgress;
  228.     RgnHandle        mouseRgn;
  229.  
  230.     InitSys();
  231.     InitApp();
  232.     
  233.     gameWasInProgress = false;    // initialize status-change sniffer
  234.     mouseRgn = NewRgn();    // allocate region for cursor maintenance
  235.     
  236.     while (true) {
  237.     
  238.         // if game ends, kill hammer cursor, show scores
  239.         if (gGameInProgress != gameWasInProgress) 
  240.             AdjustCursor(CurMousePoint(), mouseRgn); // force cursor
  241.         if (gameWasInProgress && !gGameInProgress)    // re-evaluation 
  242.             ShowScores();
  243.             
  244.         gameWasInProgress = gGameInProgress;
  245.  
  246.         IdleConn(&gOnline);        // give connection manager time
  247.         if (!gOnline)            // can't be in progress if offline
  248.             gGameInProgress = false;
  249.         IdleGame(&gGameInProgress);    // give time to deal with messages
  250.         AdjustMenus();
  251.  
  252.         // main event loop - all the usual stuff
  253.         if (WaitNextEvent(everyEvent, &theEvent, 0, mouseRgn)) 
  254.             if (!ConnEvent(&theEvent)) { // give tool chance to handle
  255.                 switch (theEvent.what) {
  256.                 case activateEvt:    // pass event to tool
  257.                     ConnActivate((WindowPtr)theEvent.message, 
  258.                         theEvent.modifiers & activeFlag);
  259.                     break;
  260.                 case updateEvt:
  261.                     if ((WindowPtr)theEvent.message == gGameWindow)
  262.                         UpdateGameWindow();
  263.                     AdjustCursor(CurMousePoint(), mouseRgn); 
  264.                     break;    // force re-evaluation of cursor
  265.                 case keyDown:
  266.                 case autoKey:
  267.                     if (theEvent.modifiers & cmdKey)
  268.                         DoMenu(MenuKey(theEvent.message & 
  269.                             charCodeMask));
  270.                     break;
  271.                 case mouseDown:
  272.                     switch (FindWindow(theEvent.where, 
  273.                         &whichWindow)) {
  274.                     case inMenuBar:
  275.                         DoMenu(MenuSelect(theEvent.where));
  276.                         break;
  277.                     case inSysWindow:
  278.                         SystemClick(&theEvent, whichWindow);
  279.                         break;
  280.                     case inGoAway:
  281.                         if (TrackGoAway(whichWindow, theEvent.where))
  282.                             DoQuit();
  283.                         break;
  284.                     case inDrag:
  285.                         DragWindow(whichWindow, theEvent.where, 
  286.                             &qd.screenBits.bounds);
  287.                         break;
  288.                     case inContent:
  289.                         if (gGameInProgress)
  290.                             HandleWindowClick(theEvent.where, 
  291.                                 &gGameInProgress);
  292.                         break;
  293.                     default:
  294.                         break;
  295.                     }
  296.                     break;
  297.                 case osEvt:    // Allow tool to handle resume event
  298.                     if (theEvent.message & kResumeMask)
  299.                         ConnResume(FrontWindow(), theEvent.message & 
  300.                             suspendResumeMessage);
  301.                     AdjustCursor(theEvent.where, mouseRgn);
  302.                     break;    // Adjust for all OS Events 
  303.                 }                // (not just mouseMoved) 
  304.             }
  305.     }
  306. }